package com.cn.yugan.Fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.cn.yugan.Activity.ArticleInfoActivity;
import com.cn.yugan.Activity.UpinfoActivity;
import com.cn.yugan.Engine.Beans.ArticleListInfo;
import com.cn.yugan.Engine.Beans.UserInfo;
import com.cn.yugan.Utils.IntentUtils;
import com.cn.yugan.Utils.LogUtils;
import com.cn.yugan.Widget.MyLinearLayoutManager;
import com.cn.yugan.mobile.R;
import com.cn.yugan.Widget.NestedView.RecycleView.CommonAdapter;
import com.cn.yugan.Widget.NestedView.RecycleView.ViewHolder;
import com.squareup.picasso.Picasso;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by Rover on 2016/8/18 .
 * Email 357319517@qq.com
 */

public class UpinfoFragment extends Fragment {
    public static final int TYPE_UPINFO = 0;
    public static final int TYPE_ARTICLE = 1;
    public static final String TITLE = "title";
    private String mTitle = "Defaut Value";
    private RecyclerView mRecyclerView;
    private CommonAdapter<Object> madapter;
    // private TextView mTextView;
    //UserInfo ArticleListInfo
    private List<Object> mDatas = new ArrayList<>();
    private int type;
    Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            madapter.notifyDataSetChanged();
            super.handleMessage(msg);
        }
    };

    public UpinfoFragment(int type) {
        this.type = type;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mTitle = getArguments().getString(TITLE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_upinfotab, container, false);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.frag_innerscrollview);
//        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new MyLinearLayoutManager(getContext()));
//        mRecyclerView.setLayoutManager(manager);
        int layoutId = 0;
        if (type == 0) {
            layoutId = R.layout.list_item_collection;
            mDatas.add(new ArticleListInfo(0, "1", "haha"));
        } else if (type == 1) {
            layoutId = R.layout.list_item_mark;
            mDatas.add(new UserInfo("haha", "haha", ""));
        }
        CreatAdapter(layoutId);
        mRecyclerView.setAdapter(madapter);
        return view;
    }

    private void CreatAdapter(final int layoutId) {
        madapter = new CommonAdapter<Object>(getActivity(), layoutId, mDatas) {
            @Override
            public void convert(ViewHolder holder, final Object o) {
                if (layoutId == R.layout.list_item_collection) {
                    holder.setOnClickListener(R.id.list_item_collection_rl, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(getContext(), ArticleInfoActivity.class);
                            intent.putExtra("aid", ((ArticleListInfo) o).getAid());
                            startActivity(intent);
                        }
                    });
                    holder.setText(R.id.list_item_collection_aid, (traStatus(((ArticleListInfo) o).getStatus())));
                    TextView textview = holder.getView(R.id.list_item_collection_aid);
                    switch (((ArticleListInfo) o).getStatus()) {
                        case 0: {
                            textview.setTextColor(getContext().getResources().getColor(R.color.tianyiblue));
                            break;
                        }
                        case 1: {
                            textview.setTextColor(getContext().getResources().getColor(R.color.color_darkgreen));
                            break;
                        }
                        case 2: {
                            textview.setTextColor(Color.RED);
                            break;
                        }
                    }
                    holder.setText(R.id.list_item_collection_title, (((ArticleListInfo) o).getTitle()));
                    holder.setText(R.id.list_item_collection_time, ("yg" + ((ArticleListInfo) o).getAid()));
                } else if (layoutId == R.layout.list_item_mark) {
                    holder.setOnClickListener(R.id.list_item_mark_ll, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(getContext(), UpinfoActivity.class);
                            LogUtils.d("uid====>" + ((UserInfo) o).getUid());
                            intent.putExtra("uid", ((UserInfo) o).getUid());
                            startActivity(intent);
                        }
                    });
                    holder.setText(R.id.markitem_name_tv, ((UserInfo) o).getUsername());
                    holder.setText(R.id.markitem_intro_tv, ((UserInfo) o).getIntro());
                    holder.setImageBitmap(R.id.markitem_cover_user_photo, null);
                    LogUtils.d(((UserInfo) o).getUsername() + "  ==>" + ((UserInfo) o).getImg());
                    if (((UserInfo) o).getImg() == null||((UserInfo) o).getImg().isEmpty()||((UserInfo) o).getImg().equals("")) {
                        Picasso.with(getContext()).load(R.mipmap.default_img).into((ImageView) holder.getView(R.id.markitem_cover_user_photo));
                    }else {
                    Picasso.with(getContext()).load(getContext().getString(R.string.imgserverurl) + ((UserInfo) o).getImg().replace("?", "!userface75?")).error(R.mipmap.default_img).into((ImageView) holder.getView(R.id.markitem_cover_user_photo));
                }}
            }
        };
    }

//    public static UpinfoFragment newInstance(String title, int ty) {
//        UpinfoFragment tabFragment = new UpinfoFragment();
//        Bundle bundle = new Bundle();
//        bundle.putString(TITLE, title);
//        type = ty;
//        tabFragment.setArguments(bundle);
//        return tabFragment;
//    }


    public void setdata(ArrayList<Object> mDatas) {
        this.mDatas.clear();
        this.mDatas.addAll(mDatas);
        mhandler.sendEmptyMessage(0);
    }

    public String traStatus(int status) {
        switch (status) {
            case 0: {
                // orlItemHolder.status.setTextColor(context.getResources().getColor(R.color.tianyiblue));
                return "待审核";
            }
            case 1: {
                // orlItemHolder.status.setTextColor(context.getResources().getColor(R.color.color_darkgreen));
                return "通过";
            }
            case 2: {
                // orlItemHolder.status.setTextColor(Color.RED);
                return "未通过";
            }
            default:
                return "";
        }
    }
}
